<template>
  <div id="home" class="wrapper">
    <nav-bar class="home-nav"><div slot="center">购物街</div></nav-bar>
    
     <tab-control class="tab-control" 
                  :titles="['流行', '新款', '精选']"
                  @tabClick="tabClick"
                  ref="tabControl1"
                  v-show="isTabFixed"
                  ></tab-control>

    <!-- Scroll.vue发出事件 用@scroll来监听事件 -->
    <!-- :probe-type="3" 如果不写冒号,传过去的数字会为string类型 -->
    <scroll class="content" 
            ref="scroll" 
            :probe-type="3" 
            @scroll="contentScroll"
            :pull-up-load="true"
            @pullingUp="loadMore">
      <home-swiper :banners="banners" @swiperImageLoad="swiperImageLoad"></home-swiper>
      <recommend-view :recommends="recommends"></recommend-view>
      <feature-view></feature-view>
      <tab-control class="tab-control" 
                  :titles="['流行', '新款', '精选']"
                  @tabClick="tabClick"
                  ref="tabControl2"
                  ></tab-control>
      <goods-list :goods="showGoods"></goods-list>
    </scroll>
    <back-top @click.native="backClick" v-show="isShowBacktop"></back-top>
    
  </div>
</template>

<script>

import HomeSwiper from './childComps/HomeSwiper.vue'
import RecommendView from './childComps/RecommendView.vue'
import FeatureView from './childComps/FeatureView.vue'

import NavBar from 'components/common/navbar/NavBar'
import TabControl from 'components/content/tabControl/TabControl'
import GoodsList from 'components/content/goods/GoodsList'
import Scroll from 'components/common/scroll/Scroll'
import BackTop from 'components/content/backTop/BackTop'


import {getHomeMultidata, getHomeGoods} from 'network/home'
import {debounce} from 'common/utils'
import {itemListenerMiXin} from 'common/mixin'



  export default {
    name: "Home",
    components: {
      NavBar,
      HomeSwiper,
      RecommendView,
      FeatureView,
      TabControl,
      GoodsList,
      Scroll,
      BackTop
       
    },
    mixins: [itemListenerMiXin],
    data() {
      return {
        banners: [],
        recommends: [],
        goods: {
          'pop': {page: 0, list: []},
          'new': {page: 0, list: []},
          'sell': {page: 0, list: []},
        },
        //默认数据
        currentType: 'pop',
        //默认小图标不显示
        isShowBacktop: false,
        tabOffsetTop: 0,
        isTabFixed: false,
        saveY: 0,
        itemImgListener: null
      }
    },
    computed: {
      showGoods() {
        return this.goods[this.currentType].list
      }
    },
    activated() {
      // 此处也可以给Scroll.vue写上scrollTo方法
      this.$refs.scroll.scroll.scrollTo(0, this.saveY, 0)
      this.$refs.scroll.refresh();
    },
    deactivated() {
      // 保存Y值
      this.saveY = this.$refs.scroll.scroll.y

      // 取消全局事件监听
      this.$bus.$off('itemImageLoad', this.itemImgListener)
    },
    created() {
      //1.请求多个数据
      this.getHomeMultidata()

      //2.请求商品数据
      this.getHomeGoods('pop')
      this.getHomeGoods('new')
      this.getHomeGoods('sell')

     
    },
    mounted() {
       //3.监听item中图片加载完成
      /**
       * bug已解决
       * 通过$bus 监听GoodsLististItem发射出来的事件  
       * 并通过 @load="imageLoad"监听每一张图片是否加载完成 
       * 加载完成后就执行依次fresh 滚动区域的高度就会刷新
       * 
       * 此处也可以在 Sroll.vue中methods里面写一个方法
       * refresh() {
       *  this.scroll.refresh()
       * }
       * 调用: this.$refs.scroll.refresh()
       */

      // const newRefresh = debounce(this.$refs.scroll.refresh, 100)

      // // 对监听的时间进行保存
      // this.itemImgListener = () => {
      //   newRefresh();
      // }
      // this.$bus.$on('itemImageLoad', this.itemImgListener)

      // 此处使用mixin,注释原来的代码
     
    },
    methods: {
      /**
       * 事件监听相关方法
       */
      

      //这里的index是子组件传过来的
      tabClick(index) {
        switch(index) {
          case 0:
            this.currentType = 'pop';
            break;
          case 1:
            this.currentType = 'new';
            break;
          case 2: 
            this.currentType = 'sell';
            break;
        }
        this.$refs.tabControl1.currentIndex = index;
        this.$refs.tabControl2.currentIndex = index;
      },
      //回到顶部
      backClick() {
        this.$refs.scroll.scroll.scrollTo(0, 0)
        // this.$refs.scroll.scrollTo(0, 0)
        //这里本来是Scroll里 调用方法 一直报错 
        //bug已解决 methods单词拼写正确
      },
      contentScroll(position) {
        //判断backTop小图标是否显示
        this.isShowBacktop = (-position.y) > 1000

        //决定tabControl是否吸顶(position: fixed)
        //注意: 这里是设置了标签 复制了一份tabControl 然后把isFixed的值赋值给v-show
        this.isTabFixed = (-position.y) > this.tabOffsetTop
      },
      //上垃加载更多
      loadMore() {
        this.getHomeGoods(this.currentType)
      },
      swiperImageLoad() {
        //获取TabControl的offSetTop
        //所有的组件都有一个属性$el,用于获取组件中的元素
        this.tabOffsetTop = this.$refs.tabControl2.$el.offsetTop;
      },

      /**
       * 网络请求相关方法
       */
      getHomeMultidata() {
         getHomeMultidata().then(res => {
          // console.log(res);
          //把请求到的数据保存起来
          // this.result = res;
          this.banners = res.data.banner.list;
          this.recommends = res.data.recommend.list;
        })
      },
      getHomeGoods(type) {
        //多次请求时 page + 1
        const page = this.goods[type].page + 1

        getHomeGoods(type, page).then(res => {
          // console.log(res);
          this.goods[type].list.push(...res.data.list)
          this.goods[type].page += 1;

          //完成上拉加载更多
          this.$refs.scroll.finishPullUp()
        })
      }

    }
  }
</script>

<style scoped>
  #home {
    /* padding-top: 44px; */
    height: 100vh;

    position: relative;
  }

  .home-nav {
    background: var(--color-tint);
    color: #fff;

    /* 这个属性在浏览器使用原生滚动时使用  */
    /* position: fixed;
    left: 0;
    right: 0;
    top: 0;
    z-index: 9; */
  }

  .tab-control {
    position: relative;
    /* top: 44px; */
    z-index: 9;
  }


.content {
  overflow: hidden;

  position: absolute;
  top: 44px;
  bottom: 49px;
}



  /* .content {
    height: cacl(100% - 98px);
    height: 400px;
  } */
</style>
